package com.chushouya.manager.dao.repository;

import com.general.framework.core.lang.Longs;
import com.general.framework.core.lang.Strings;
import com.general.framework.core.schema.PageResult;
import com.general.framework.data.mybatis.BaseRepository;
import com.chushouya.manager.dao.entity.KefuEntity;
import com.chushouya.manager.dao.mapper.KefuEntityMapper;
import com.chushouya.manager.dto.admin.kefu.KefuQuery;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Repository
public class KefuRepository extends BaseRepository<KefuEntityMapper, KefuEntity, Long> {
    public KefuEntity selectOne(KefuQuery query) {
        return this.selectFirstOneByExample(this.buildExample(query));
    }

    private Example buildExample(KefuQuery query) {
        Example example = this.buildExample();
        if (Longs.notNullAndZero(query.getKefuId())) {
            example.and().andEqualTo("kefuId", query.getKefuId());
        }
        if (Strings.isNotEmpty(query.getKefuName())) {
            example.and().andLike("kefuName", Strings.format("%{}%", query.getKefuName()));
        }
        example.and().andEqualTo("defFlag", 0);
        example.orderBy("kefuId").desc();
        return example;
    }

    public PageResult<KefuEntity> selectPageResult(KefuQuery query) {
        return this.selectPage(query.getPageRequest(), this.buildExample(query));
    }

    public List<KefuEntity> selectList(KefuQuery query) {
        return this.selectByExample(this.buildExample(query));
    }

}
